#!/usr/bin/python
# coding:utf-8

import flask
from flask import Flask, url_for, render_template, request, make_response, session
from flask_login import login_required, current_user
from flask_session import Session
from flask_wtf.csrf import CSRFProtect

import auth
import demo
import rest

import logging

app = Flask(__name__, static_folder='../web4umc/dist', static_url_path='')
flask.config.app = app

# 使用os.urandom(12)生成的随机密钥
app.secret_key = '\t=(W\x8dj.[U\xbb\xb1?'

# Check Configuration section for more details
SESSION_TYPE = 'redis'
app.config.from_object(__name__)
Session(app)

# csrf protection
# csrf = CSRFProtect()
# csrf.init_app(app)

auth.init_app(app, prefix='/oa')

demo.init_app(app, prefix='/demo')

rest.init_app(app)


@app.before_request
def before_request():
    print('root %s' % request.url)


@app.errorhandler(404)
def not_found(error):
    return render_template('error.html', name=error.name), 404


@app.route('/')
@login_required
def index():
    return flask.redirect(url_for('static', filename='index.html'))


@app.route('/echo/<string:msg>')
def echo(msg):
    return 'echo: (%s)' % msg


@app.route("/p/<path:pp>")
def do_path(pp):
    print(url_for('do_path', pp='asd/ddd'))
    return "the path: %s" % pp


@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
    print(session)

    flask.flash('hello')

    resp = make_response(render_template('hello.html', name=name or current_user.username))

    return resp


@app.route('/test')
def test():
    return app.send_static_file('index.html')


if __name__ == '__main__':
    # app.debug = True
    handler = logging.FileHandler('flask.log')
    app.logger.addHandler(handler)

    app.run(host='0.0.0.0', port=5000)
